package malus.shaofeng.com.beiwomusic.utils;

import android.content.Context;
import android.os.Environment;

import com.lidroid.xutils.BitmapUtils;

import java.io.File;

/**
 * Created by Administrator on 2015/3/10.
 */
public class BitmapHelper {
    private static BitmapUtils utils;
    private BitmapHelper(Context context){

    }
    public static void initBitmapHelper(Context context){
        if(context!=null){
            File file=new File(Environment.getExternalStorageDirectory(),Constants.File.BITMAP_HELPER_CACHE);
            utils=new BitmapUtils(context,file.getAbsolutePath(),0.5f);
            utils.configDiskCacheEnabled(true);
            utils.configMemoryCacheEnabled(true);
        }else{
            throw new IllegalArgumentException("context must not null");
        }
    }
    public static BitmapUtils getBitmapUtils(){
        if(utils!=null){
            return utils;
        }else{
            throw new IllegalArgumentException("Bitmap must init before use");
        }
    }

    /**
     * 仅在被窝音乐中获取图片有用，可以将类似
     * /mnt/res.nmting.com/res/qms/icon/20140910/jpg/2e/d9/2ed98d7d154f43f299c4eca3fd01cd9b.jpg
     * 的图片地址更改为
     * BASE+/res/qms/icon/20140910/jpg/2e/d9/2ed98d7d154f43f299c4eca3fd01cd9b.jpg
     * @param imageUrl
     * @return
     */
    public static String getImageUrl(String imageUrl) {
        String path = "";
        if (imageUrl != null) {
            String[] split = imageUrl.split("/");
            for (int j = 3; j < split.length; j++) {
                path = path + split[j];
                if (j != split.length - 1) {
                    path += "/";
                }
            }
            path = Constants.URL.IMAGE_URL + path;
        }
        return path;
    }
}
